//
//  NKLabel.m
//  Dispatch
//
//  Created by hyeyeon on 14. 1. 16..
//  Copyright (c) 2014년 hyeyeon. All rights reserved.
//

#import "NKLabel.h"

@implementation NKLabel

- (id)init
{
    self = [super init];
    if (self) {
        // NSMutableAttributedString 변수 할당
        _attrString = [[NSMutableAttributedString alloc] init];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // NSMutableAttributedString 변수 할당
        _attrString = [[NSMutableAttributedString alloc] init];
        
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    if (self.text) {
        // 화면에 그리는 코드
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSaveGState(ctx);
        CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, self.bounds.size.height), 1.f, -1.f));
		
        if (self.shadowColor) {
            CGContextSetShadowWithColor(ctx, self.shadowOffset, 0.0, self.shadowColor.CGColor);
        }
        
        CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)_attrString);
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathAddRect(path, NULL, self.bounds);
        CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path, NULL);
        
        CTFrameDraw(textFrame, ctx);
        CGContextRestoreGState(ctx);
        
        CGPathRelease(path);
        CFRelease(framesetter);
        CFRelease(textFrame);
        
    }else {
        [super drawTextInRect:rect];
    }
}
- (void)setNeedsDisplay {
    [super setNeedsDisplay];
}

- (void)setText:(NSString *)text {
    // setText를 오버라이딩
    // NSMutableAttributedString에 setSting을 해줍니다.
    [[_attrString mutableString] setString:text];
    
    [super setText:text];   // will call setNeedsDisplay
}

- (void)setTextColor:(UIColor*)color {
    // setTextColor를 오버라이딩
    // NSMutableAttributedString에 Attribute를 이용합니다.
    // kCTForegroundColorAttributeName 키를 이용하면 글자색을 바꿀수가 있습니다.
    // 여기선 전체색이기 때문에 range를 전체로 잡습니다.
    if (_attrString) {
        [_attrString removeAttribute:(NSString*)kCTForegroundColorAttributeName
                               range:NSMakeRange(0,[_attrString length])];
        [_attrString addAttribute:(NSString*)kCTForegroundColorAttributeName
                            value:(id)color.CGColor
                            range:NSMakeRange(0,[_attrString length])];
    }
    
    [super setTextColor:color]; // will call setNeedsDisplay
}

- (void)setColorWord:(NSString *)word withColor:(UIColor *)color {
    // kCTForegroundColorAttributeName 키를 이용하여...
    // 글자를 찾아서 그 글자만 색을 바꾸게 합니다.
    if (_attrString) {
        [_attrString removeAttribute:(NSString*)kCTForegroundColorAttributeName
                               range:[[_attrString mutableString] rangeOfString:word]];
        [_attrString addAttribute:(NSString*)kCTForegroundColorAttributeName
                            value:(id)color.CGColor
                            range:[[_attrString mutableString] rangeOfString:word]];
    }
    
    // setColorWord함수는 직접 만든 함수 이기 때문에 setNeedsDisplay 호출해야 합니다.
    [self setNeedsDisplay];
}

- (void)setFont:(UIFont*)font {
    // setFont함수를 오버라이딩
    // kCTFontAttributeName 키를 이용하여 전체 폰트와 폰트 크기 변경할 수 있습니다.
    if (_attrString) {
        CTFontRef aFont = CTFontCreateWithName((CFStringRef)font.fontName , font.pointSize, NULL);
        [_attrString removeAttribute:(NSString*)kCTFontAttributeName
                               range:NSMakeRange(0,[_attrString length])];
        [_attrString addAttribute:(NSString*)kCTFontAttributeName
                            value:(__bridge id)aFont
                            range:NSMakeRange(0,[_attrString length])];
        CFRelease(aFont);
    }
    
    [super setFont:font];
}

- (void)setUnderLine:(BOOL)flag {
    // kCTUnderlineStyleAttributeName 키를 이용하여 언더라인 표시
    // 값은 언더라인스타일로 지정 : kCTUnderlineStyleSingle, kCTUnderlineStyleNone 등..
    if (flag) {
        if (_attrString) {
            [_attrString removeAttribute:(NSString *)kCTUnderlineStyleAttributeName
                                   range:NSMakeRange(0,[_attrString length])];
            [_attrString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
                                value:(id)[NSNumber numberWithInt:kCTUnderlineStyleSingle]
                                range:NSMakeRange(0,[_attrString length])];
        }
    }else {
        if (_attrString) {
            [_attrString removeAttribute:(NSString *)kCTUnderlineStyleAttributeName
                                   range:NSMakeRange(0,[_attrString length])];
            [_attrString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
                                value:(id)[NSNumber numberWithInt:kCTUnderlineStyleNone]
                                range:NSMakeRange(0,[_attrString length])];
        }
    }
    
    [self setNeedsDisplay];
}

- (void)setFontWord:(NSString *)word withFont:(UIFont*)font {
    if (_attrString) {
        CTFontRef aFont = CTFontCreateWithName((CFStringRef)font.fontName , font.pointSize, NULL);
        [_attrString removeAttribute:(NSString*)kCTFontAttributeName
                               range:[[_attrString mutableString] rangeOfString:word]];
        [_attrString addAttribute:(NSString*)kCTFontAttributeName
                            value:(__bridge id)aFont
                            range:[[_attrString mutableString] rangeOfString:word]];
        CFRelease(aFont);
    }
    
    [super setFont:font];
}


@end
